package logic

import (
	//"bluebell/dao/mysql"
	//"bluebell/dao/redis"
	//"bluebell/models"
	//snow "bluebell/pkg/sonwflake"

	"go.uber.org/zap"
	"lottery/web/blue/bluebell/dao/mysql"
	"lottery/web/blue/bluebell/dao/redis"
	"lottery/web/blue/bluebell/models"
	snow "lottery/web/blue/bluebell/pkg/sonwflake"
)

func CreatePost(p *models.Post) (err error) {
	// 1. 生成post id 依然使用雪花算法
	p.ID = snow.GenID()
	// 2. 保存到数据库
	err = mysql.CreatePost(p)
	if err != nil {
		return err
	}
	// 3. 将当前的帖子创建时间保存到 redis中，后面的帖子投票需要用到
	err = redis.CreatePost(p.ID, p.CommunityID)
	return
}

func GetPostById(pid int64) (data *models.ApiPostDetail, err error) {
	// 查询帖子信息
	post, err := mysql.GetPostById(pid)
	if err != nil {
		zap.L().Error("mysql.GetPostById() failed",
			zap.Int64("pid", pid),
			zap.Error(err))
		return
	}
	// 查询作者信息
	user, err := mysql.GetUserById(post.AuthorID)
	if err != nil {
		zap.L().Error("mysql.GetUserById() failed",
			zap.Int64("author_id", post.AuthorID),
			zap.Error(err))
		return
	}
	// 查询社区信息
	community, err := mysql.GetCommunityDetailByID(post.CommunityID)
	if err != nil {
		zap.L().Error("mysql.GetCommunityDetailByID(post.CommunityID)",
			zap.Int64("community_id", post.CommunityID),
			zap.Error(err))
		return
	}
	// 填充数据
	data = &models.ApiPostDetail{
		AuthorName:      user.Username,
		VoteNum:         0,
		Post:            post,
		CommunityDetail: community,
	}
	return
}

func Count() int64 {
	return mysql.Count()
}

func CountC(pid int64) int64 {
	return mysql.CountC(pid)
}

func GetPostList() (data []*models.ApiPostDetail, err error) {
	// 获取投票列表
	posts, err := mysql.GetPostList()
	if err != nil {
		zap.L().Error("mysql.GetPostList() failed",
			zap.Error(err))
		return
	}
	// 初始化结果集
	data = make([]*models.ApiPostDetail, 0, len(posts))
	// 循环添加数据
	for _, post := range posts {
		user, err := mysql.GetUserById(post.AuthorID)
		if err != nil {
			zap.L().Error("mysql.GetUserById() failed",
				zap.Int64("author_id", post.AuthorID),
				zap.Error(err))
			continue
		}
		// 查询社区信息
		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
		if err != nil {
			zap.L().Error("mysql.GetCommunityDetailByID(post.CommunityID)",
				zap.Int64("community_id", post.CommunityID),
				zap.Error(err))
			continue
		}
		// 拼接数据
		postDetail := &models.ApiPostDetail{
			AuthorName:      user.Username,
			VoteNum:         0,
			Post:            post,
			CommunityDetail: community,
		}
		// 存入data
		data = append(data, postDetail)
	}
	return
}

// 配合redis获取帖子列表(根据 参数，时间顺序或者分数设置返回)
func GetPostList2(p *models.ParamPostList) (data []*models.ApiPostDetail, err error) {
	// 2. 根据redis查询 帖子id列表
	ids, err := redis.GetPostIDsInOrder(p)
	// 3. 根据id，去数据库查询帖子详细信息。填充作者和分区的信息
	if err != nil {
		return
	}
	if len(ids) == 0 {
		zap.L().Warn("redis.GetPostIDsInOrder(p) return 0 data")
		return
	}
	posts, err := mysql.GetPostListByIDs(ids)
	if err != nil {
		return
	}
	// 		4. 提前查询好每篇帖子的投票数。根据redis
	voteData, err := redis.GetPostVoteData(ids)
	if err != nil {
		return
	}
	// 初始化结果集
	data = make([]*models.ApiPostDetail, 0, len(posts))
	// 循环添加数据
	for idx, post := range posts {
		user, err := mysql.GetUserById(post.AuthorID)
		if err != nil {
			zap.L().Error("mysql.GetUserById() failed",
				zap.Int64("author_id", post.AuthorID),
				zap.Error(err))
			continue
		}
		// 查询社区信息
		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
		if err != nil {
			zap.L().Error("mysql.GetCommunityDetailByID(post.CommunityID)",
				zap.Int64("community_id", post.CommunityID),
				zap.Error(err))
			continue
		}
		// 拼接数据
		postDetail := &models.ApiPostDetail{
			AuthorName:      user.Username,
			VoteNum:         voteData[idx],
			Post:            post,
			CommunityDetail: community,
		}
		// 存入data
		data = append(data, postDetail)
	}
	return
}

func GetCommunityPostList2(p *models.ParamPostList) (data []*models.ApiPostDetail, err error) {
	// 2. 根据redis查询 帖子id列表
	ids, err := redis.GetCommunityPostIDsInOrder(p)
	// 3. 根据id，去数据库查询帖子详细信息。填充作者和分区的信息
	if err != nil {
		return
	}
	if len(ids) == 0 {
		zap.L().Warn("redis.GetPostIDsInOrder(p) return 0 data")
		return
	}
	posts, err := mysql.GetPostListByIDs(ids)
	if err != nil {
		return
	}
	// 		4. 提前查询好每篇帖子的投票数。根据redis
	voteData, err := redis.GetPostVoteData(ids)
	if err != nil {
		return
	}
	// 初始化结果集
	data = make([]*models.ApiPostDetail, 0, len(posts))
	// 循环添加数据
	for idx, post := range posts {
		user, err := mysql.GetUserById(post.AuthorID)
		if err != nil {
			zap.L().Error("mysql.GetUserById() failed",
				zap.Int64("author_id", post.AuthorID),
				zap.Error(err))
			continue
		}
		// 查询社区信息
		community, err := mysql.GetCommunityDetailByID(post.CommunityID)
		if err != nil {
			zap.L().Error("mysql.GetCommunityDetailByID(post.CommunityID)",
				zap.Int64("community_id", post.CommunityID),
				zap.Error(err))
			continue
		}
		// 拼接数据
		postDetail := &models.ApiPostDetail{
			AuthorName:      user.Username,
			VoteNum:         voteData[idx],
			Post:            post,
			CommunityDetail: community,
		}
		// 存入data
		data = append(data, postDetail)
	}
	return
}

func GetPostListNew(p *models.ParamPostList) (data []*models.ApiPostDetail, count int64, err error) {
	// 查所有的
	if p.CommunityID == 0 {
		data, err = GetPostList2(p)
		count = Count()
	} else {
		//	根据社区查
		data, err = GetCommunityPostList2(p)
		count = CountC(p.CommunityID)
	}
	if err != nil {
		zap.L().Error("logic.getPostListNew failed", zap.Error(err))
	}
	return
}

func GetSomePost(com string) []*models.Post {
	return mysql.GetSomePost(com)
}
